#include <iostream>
#include "Des.h"

void TestExt();
void TestMac();

int main()
{
    TestMac();
}

void TestExt()
{
    CDes des;
    std::string bsHostRnd = "D4DCA09B01A881B5";
    // std::string bsResp =
    // "000000000000499910DBFF020006694279E700CB3FE04F86F0241625";
    std::string bsResp;
    std::cout << "Input:";
    std::getline(std::cin, bsResp);
    std::string bsCardRnd = bsResp.substr(12 * 2, 8 * 2);
    std::string bsCardInx = bsCardRnd.substr(0 * 2, 2 * 2);
    std::string KDCenc = "404142434445464748494A4B4C4D4E4F";
    std::string KDCmac = "404142434445464748494A4B4C4D4E4F";
    std::string bsDiv2 = "0182" + bsCardInx + "000000000000000000000000";
    std::string bsDiv4 = "0101" + bsCardInx + "000000000000000000000000";
    std::string KSCenc;
    std::string KSCmac;
    des.fnb3Des_CBC(KDCenc, bsDiv2, KSCenc);
    des.fnb3Des_CBC(KDCmac, bsDiv4, KSCmac);
    std::string bsRnd = bsCardRnd + bsHostRnd + "80";
    bsRnd.append(16 - bsRnd.size() % 16, '0');
    std::string bsEncCardRnd;
    des.fnb3Des_CBC(KSCenc, bsRnd, bsEncCardRnd);
    bsEncCardRnd = bsEncCardRnd.substr(bsEncCardRnd.size() - 16, 16);
    std::cout << bsEncCardRnd << std::endl;
    std::string bsEAApdu = "8482000010" + bsEncCardRnd;
    std::string bsCMAC;
    des.fnbMAC_3DES_FT(KSCmac, "0000000000000000", bsEAApdu, bsCMAC);
    std::cout << bsCMAC << std::endl;

    std::cout << "Result:"
              << "8482000010" << bsEncCardRnd << bsCMAC << std::endl;
}

void TestMac()
{
    CDes des;
    std::string key = "f555ec8c4f15bd5132be9afa5a827b1a";
    std::string random = "9023560900000000";
    std::string input =
        "04d69d0024FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
        "FFFF";
    std::string MAC = "E6394A68";
    std::string result;
    des.fnbMAC_3DES(key, random, input, result);
    std::cout << result << std::endl;
    des.fnbMAC_3DES_FT(key, random, input, result);
    std::cout << result << std::endl;
}